/**
 * Created by jianzhao on 2016/6/3.
 */
'use strict';
import {
    StyleSheet,
    Text,
    View,
    WebView,
    Dimensions,
    Image,
    PixelRatio,
    Alert
} from 'react-native';
import React,{Component} from 'react';
import NavTitle from '../component/NavTitle';

var _status = true;

class MapSelect extends React.Component {
    // 构造
    constructor(props) {
        super(props);
        this.state={

        }
    }

    onNavigationStateChange(navState){
        if(/latng=[\d|.|,]+/g.test(navState.url)){
            let _locations = navState.url.match(/latng=[\d|.|,]+/g)[0].split('=')[1].split(',');
            this.props.getLocation(_locations[0],_locations[1]);
            if(_status){
                _status = false;
                this.props.navigator.pop();
            }
        }
    };

    componentWillUnmount(){
        _status = true;
    }

    render() {
        let _pos = '25.049214,102.703629';
        if(this.props.lat){
            _pos = this.props.lat + ','+this.props.lng;
        }
        return (
            <View style={{flex:1,flexDirection:'column',backgroundColor:'#FFF',width:Dimensions.get('window').width}}>
                <NavTitle
                    headColor={'#fbdc3a'}
                    title={'地图选择'}
                    leftButton={true}
                    leftImg={require('../imgs/left.png')}
                    leftText = {'返回'}
                    leftHander={()=> {
                        this.props.navigator.pop();
                    }}
                />

                <WebView
                    style={{width:Dimensions.get('window').width,flex:1,}}
                    source ={{uri:`https://3gimg.qq.com/lightmap/components/locationPicker2/index.html?search=1&type=0&coord=${_pos}&backurl=http%3A%2F%2F3gimg.qq.com%2Flightmap%2Fcomponents%2FlocationPicker2%2Fback.html&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&referer=myapp`}}
                    javaScriptEnabled={true}
                    onNavigationStateChange={this.onNavigationStateChange.bind(this)}
                >
                </WebView>

            </View>
        );
    }
};

var styles = StyleSheet.create({

});

module.exports = MapSelect;